<template>
    <div>
        <!--    面包屑导航区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-button type="primary" @click="addCate">添加分类</el-button>
            <tree-table :data="this.cateList" :columns="columns" show-index index-text="#" border :selection-type="false" :expand-type="false" :show-row-hover="false" style="margin-top: 15px">
        <!--是否有效列模板-->
                <template slot="isok" slot-scope="scope">
                    <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color: lightgreen"></i>
                    <i class="el-icon-error" v-else style="color: red"></i>
                </template>
<!--                排序列模板-->
                <template slot-scope="scope" slot="order">
                    <el-tag v-if="scope.row.cat_level==0">一级</el-tag>
                    <el-tag type="success" v-else-if="scope.row.cat_level==1">二级</el-tag>
                    <el-tag type="warning" v-else>三级</el-tag>
                </template>
<!--                操作列模板-->
                <template slot-scope="scope" slot="opt">
                    <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
                    <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                </template>
            </tree-table>
<!--            分页栏-->
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="queryInfo.pagenum"
                    :page-sizes="[3, 5, 10, 15]"
                    :page-size="queryInfo.pagesize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </el-card>
<!--        添加分类的对话框-->
        <el-dialog title="添加分类" :visible.sync="cateDialogVisible" width="30%" @close="addCateDiaClose">
            <el-form ref="addCateFormRef" :model="addCateForm" label-width="100px" :rules="addCateRules">
                <el-form-item label="分类名称:" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类:" prop="">
                    <el-cascader placeholder="请选择" clearable v-model="selectedOption" :options="cateOptions" :props="parentCateProps" @change="handleParentCateChange" style="width: 100%">
                    </el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmAddCateForm">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "cate",
        data(){
            return{
                //商品分类的数据列表
                cateList:[],
                queryInfo:{
                    type: 3,
                    pagenum: 1,
                    pagesize: 5
                },
                total:0,
                columns:[
                    {
                        label:'分类名称',
                        prop:'cat_name'
                    },
                    {
                        label:'是否有效',
                        //表示当前列定义为模板
                        type:'template',
                        //当前列的名称
                        template:'isok'
                    },
                    {
                        label:'排序',
                        type:'template',
                        template:'order'
                    },
                    {
                        label:'操作',
                        type:'template',
                        template:'opt'
                    }
                ],
                cateDialogVisible:false,
                //添加分类的提交表单
                addCateForm:{
                    //将要添加分类的名称
                    cat_name:'',
                    //父级分类的id
                    cat_pid:0,
                    //分类等级
                    cat_level:0
                },
                addCateRules:{
                    cat_name:[
                        {required:true,message:'请输入分类名称',trigger:'blur'}
                    ]
                },
                cateOptions:[],
                selectedOption:[],
                //添加分类框级联选择器配置
                parentCateProps:{
                        expandTrigger:'hover',
                        value:'cat_id',
                        label:'cat_name',
                        children:'children',
                        checkStrictly:true
                }
            }
        },
        created() {
            this.getCateList()
        },
        methods:{
            //获取商品分类数据
            getCateList(){
                this.$http.get('categories',{params:this.queryInfo}).then((res)=>{
                    if (res.data.meta.status!==200){
                        return this.$message.error('获取数据失败')
                    }
                    this.cateList=res.data.data.result
                    this.total=res.data.data.total
                })
            },
            //监听每页条数发生变化
            handleSizeChange(newSize){
                this.queryInfo.pagesize=newSize
                this.getCateList()
            },
            //监听当前页数发生变化
            handleCurrentChange(currentPage){
                this.queryInfo.pagenum=currentPage
                this.getCateList()
            },
            //点击添加分类按钮
            addCate(){
                this.$http.get('categories',{params:{type:2}}).then((res)=>{
                    this.cateOptions=res.data.data
                    
                })
                
                this.cateDialogVisible=true
            },
            //级联选择器选中项发生变化
            handleParentCateChange(){
                console.log(this.selectedOption);
            },
            //确认提交添加分类
            confirmAddCateForm(){
                if (this.selectedOption.length===0){
                    this.addCateForm.cat_pid=0
                    this.addCateForm.cat_level=0
                }else {
                    this.addCateForm.cat_pid=this.selectedOption[this.selectedOption.length-1]
                    this.addCateForm.cat_level=this.selectedOption.length
                }
                console.log(this.addCateForm)
                this.$refs.addCateFormRef.validate(valid=>{
                    if (!valid) return
                    this.$http.post('categories',this.addCateForm).then((res)=>{
                        if (res.data.meta.status==201){
                            this.$message.success('添加分类成功')
                        }
                        this.cateDialogVisible=false
                        this.getCateList()
                    })
                })
            },
            //监听添加分类对话框关闭事件
            addCateDiaClose(){
                this.$refs.addCateFormRef.resetFields()
                this.selectedOption=[]
                this.addCateForm.cat_level=0
                this.addCateForm.cat_pid=0
            }
        }  
        
    }
</script>

<style scoped>
</style>
